#if !UNITY_FLASH
#define DYNAMIC_FONT
#endif

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

[CanEditMultipleObjects]
[CustomEditor(typeof(UILabelEx), true)]
public class UILabelExInspector : UILabelInspector
{
    static string[] keys;
    int select = -1;
    static string searchText = "";
    static string[] searchKeys;

    public static void RefreshKeys()
    {
        keys = null;
        searchKeys = null;
    }

    protected override bool ShouldDrawProperties ()
    {
        UILabelEx mLabel = mWidget as UILabelEx;
		if (keys == null)
		{
			keys = GetKeys();
			select = -1;
		}

		if (keys != null)
		{
			if (select < 0)
			{
				select = GetSelectIndex(mLabel.textKey, keys);
			}
		}

		int newTextKey = 0;
		string newTextKeyName = null;
		if (ShowText(ref select, keys, out newTextKey, out newTextKeyName))
		{
			SerializedProperty sp = serializedObject.FindProperty("_textKey");
			sp.intValue = newTextKey;

			SerializedProperty nameSp = serializedObject.FindProperty("textKeyName");
			nameSp.stringValue = newTextKeyName;

			mLabel.textKey = newTextKey;

			SerializedProperty textSp = serializedObject.FindProperty("mText");
			textSp.stringValue = LangManager.Get(newTextKey);
		}

//        if (mLabel.material == null || serializedObject.isEditingMultipleObjects)
//        {
//            NGUIEditorTools.DrawProperty("Shader", serializedObject, "mShader");
//        }

        return base.ShouldDrawProperties();
    }

	public static bool ShowText(ref int select, string[] keys, out int newTextKey, out string newTextKeyName)
	{
		newTextKey = 0;
		newTextKeyName = null;

		EditorGUILayout.BeginHorizontal();
        string tempSearchText = EditorGUILayout.TextField("lang key", searchText);
        if (tempSearchText != searchText || searchKeys == null)
        {
            searchText = tempSearchText;
            if (string.IsNullOrEmpty(searchText))
            {
                searchKeys = keys;
            }
            else
            {
                List<string> keyList = new List<string>();
                for (int i = 0; i < keys.Length; i++)
                {
                    string key = keys[i];
                    if (key.Contains(searchText))
                    {
                        keyList.Add(key);
                    }
                }
                searchKeys = keyList.ToArray();
            }


        }

        int newSel = EditorGUILayout.Popup(select, searchKeys, GUILayout.ExpandWidth(true));
		if (newSel != select)
		{
			select = newSel;
            string key = searchKeys[select];
			Dictionary<string, int> keyMap = LangTool.GetKeyMap();
			int idKey = 0;
			if (!keyMap.TryGetValue(key, out idKey))
			{
				idKey = 0;
			}

			newTextKey = idKey;
			newTextKeyName = key;

			return true;
		}
		EditorGUILayout.EndHorizontal();

		return false;
	}

	public static int GetSelectIndex(int textKey, string[] keys)
	{
		int tempSelect = -1;
		Dictionary<int, string> reverseKeyMap = LangTool.GetReverseKeyMap();
		string key = null;
		if (reverseKeyMap.TryGetValue(textKey, out key))
		{
			tempSelect = System.Array.FindIndex(keys, (a) =>
				{
					return key == a;
				});

			if (tempSelect < 0)
			{
				tempSelect = 0;
			}
		}
		else
		{
			tempSelect = 0;
		}

		return tempSelect;
	}

	public static string[] GetKeys()
	{
		Dictionary<string, int> keyMap = LangTool.GetKeyMap();
		if (keyMap == null)
		{
			return null;
		}

		string[] keys = new string[keyMap.Count + 1];
		int i = 0;
		keys[i++] = "[none]";
		foreach (var entry in keyMap)
		{
			keys[i++] = entry.Key;
		}

		return keys;
	}
}
